{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "序列化的过程：pickle.dump--->从内存转存到硬盘等，\n",
    "              pickle.load--->从硬盘中读取\n",
    "json操作类似"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "%quickref"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'jerry': [1983, 'female', 'beijing'], 'rain': {'f/m': 'f', 'birth': 1981}}\n"
     ]
    }
   ],
   "source": [
    "import pickle\n",
    "accoutInfo={\"jerry\":[1983,\"female\",\"beijing\"],\n",
    "           \"rain\":{\"birth\":1982-1,\"f/m\":\"f\"}}\n",
    "print accoutInfo\n",
    "f=file('c:\\\\pythoncoding\\\\data\\\\myaccoutInfo.txt','wb')\n",
    "pickle.dumps(accoutInfo) #注意该语句与下句的区别，dumps表示返回的是一个字符串！！！！！！！！！！！！\n",
    "pickle.dump(accoutInfo,f)\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['(dp0\\n',\n",
       " \"S'jerry'\\n\",\n",
       " 'p1\\n',\n",
       " '(lp2\\n',\n",
       " 'I1983\\n',\n",
       " \"aS'female'\\n\",\n",
       " 'p3\\n',\n",
       " \"aS'beijing'\\n\",\n",
       " 'p4\\n',\n",
       " \"asS'rain'\\n\",\n",
       " 'p5\\n',\n",
       " '(dp6\\n',\n",
       " \"S'f/m'\\n\",\n",
       " 'p7\\n',\n",
       " \"S'f'\\n\",\n",
       " 'p8\\n',\n",
       " \"sS'birth'\\n\",\n",
       " 'p9\\n',\n",
       " 'I1981\\n',\n",
       " 'ss.']"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fileread=file('c:\\\\pythoncoding\\\\data\\\\myaccoutInfo.txt','r')\n",
    "fileread.readlines()#这里不适合直接用readlines，读出的数据类似乱码，应使用pickle.load"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'jerry': [1983, 'female', 'beijing'], 'rain': {'birth': 1981, 'f/m': 'f'}}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f1=file('c:\\\\pythoncoding\\\\data\\\\myaccoutInfo.txt','r')\n",
    "pickle.load(f1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'jerry': ['1983-7', 'female', 'beijing'], 'rain': {'f/m': 'f', 'birth': 1981}}\n"
     ]
    }
   ],
   "source": [
    "f2=file('c:\\\\pythoncoding\\\\data\\\\myaccoutInfo.txt','r')\n",
    "myaccoutInfo1=pickle.load(f2)\n",
    "myaccoutInfo1['jerry']=['1983-7','female','beijing']\n",
    "print myaccoutInfo1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'{\"rain\": {\"f/m\": \"m\", \"birth\": 1981}, \"Tom\": [1983, \"female\", \"shanghai\"]}'"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import json\n",
    "accout_jason={\"Tom\":[1983,\"female\",\"shanghai\"],\n",
    "           \"rain\":{\"birth\":1982-1,\"f/m\":\"m\"}}\n",
    "json.dumps(accout_jason)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "f2=file('c:\\\\pythoncoding\\\\data\\\\myaccoutInfo_json.txt','wb')\n",
    "json.dump(accout_jason,f2)\n",
    "f2.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{u'Tom': [1983, u'female', u'shanghai'],\n",
       " u'rain': {u'birth': 1981, u'f/m': u'm'}}"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f2=file('c:\\\\pythoncoding\\\\data\\\\myaccoutInfo_json.txt','r')\n",
    "json.load(f2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['1.0', '1.1', '1.2', '1.3', '2.0']"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pickle.format_version #当前版本\n",
    "pickle.compatible_formats #兼容版本\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "pickle"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
